package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.crm.dao.LinkmanDAO;
import com.feeyin.radar.crm.dao.LinkmanUpdateHistoryDAO;
import com.feeyin.radar.crm.model.Linkman;
import com.feeyin.radar.crm.service.LinkmanUpdateHistoryService;
import com.feeyin.radar.sys.dao.RecordRangeDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.RecordRange;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LinkmanUpdateHistoryServiceImpl
  implements LinkmanUpdateHistoryService
{
  private LinkmanUpdateHistoryDAO linkmanUpdateHistoryDAO;
  private LinkmanDAO linkmanDAO;
  private UserDAO userDAO;
  private RecordRangeDAO recordRangeDAO;

  public LinkmanUpdateHistoryDAO getLinkmanUpdateHistoryDAO()
  {
    return this.linkmanUpdateHistoryDAO;
  }

  public void setLinkmanUpdateHistoryDAO(LinkmanUpdateHistoryDAO linkmanUpdateHistoryDAO)
  {
    this.linkmanUpdateHistoryDAO = linkmanUpdateHistoryDAO;
  }

  public LinkmanDAO getLinkmanDAO() {
    return this.linkmanDAO;
  }

  public void setLinkmanDAO(LinkmanDAO linkmanDAO) {
    this.linkmanDAO = linkmanDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public RecordRangeDAO getRecordRangeDAO() {
    return this.recordRangeDAO;
  }

  public void setRecordRangeDAO(RecordRangeDAO recordRangeDAO) {
    this.recordRangeDAO = recordRangeDAO;
  }

  public List getPageLinkmanUpdateHistorysByCustomerIdAndLinkmanId(Integer customerId, Integer linkmanId, Page page)
  {
    List list = new ArrayList();
    list = this.linkmanUpdateHistoryDAO.getPageLinkmanUpdateHistorysByCustomerIdAndLinkmanId(customerId, linkmanId, page);

    if (list == null) return null;
    for (int i = 0; i < list.size(); i++) {
      Linkman linkman = new Linkman();
      linkman = (Linkman)list.get(i);
      linkman.setUpdatePerson(this.userDAO.getUserNameById(linkman.getUpdatePersonId()));
    }
    return list;
  }

  public Integer getPageLinkmanUpdateHistorysCountByCustomerIdAndLinkmanId(Integer customerId, Integer linkmanId)
  {
    return this.linkmanUpdateHistoryDAO.getPageLinkmanUpdateHistorysCountByCustomerIdAndLinkmanId(customerId, linkmanId);
  }

  public Linkman getLinkmanUpdateHistory(Integer id) {
    Linkman linkman = new Linkman();
    linkman = this.linkmanUpdateHistoryDAO.getLinkmanUpdateHistory(id);

    if (linkman == null) return null;
    RecordRange recordRange = null;
    if (linkman.getLinkmanDeptId() != null) {
      recordRange = new RecordRange();
      recordRange = this.recordRangeDAO.getRecordRangeById(linkman.getLinkmanDeptId());
      if (recordRange != null) {
        linkman.setLinkmanDept(recordRange.getRangeValue());
      }
    }
    if (linkman.getLinkmanDutyId() != null) {
      recordRange = new RecordRange();
      recordRange = this.recordRangeDAO.getRecordRangeById(linkman.getLinkmanDutyId());
      if (recordRange != null) {
        linkman.setLinkmanDuty(recordRange.getRangeValue());
      }
    }

    return linkman;
  }

  public boolean restoreLinkmanInfo(Integer id, Integer currentUserId) {
    if ((id == null) || (currentUserId == null)) return false;
    Linkman restoreLinkman = new Linkman();
    restoreLinkman = this.linkmanUpdateHistoryDAO.getLinkmanUpdateHistory(id);

    if (restoreLinkman == null) return false;
    restoreLinkman.setId(restoreLinkman.getLinkmanId());
    restoreLinkman.setRestorePersonId(currentUserId);
    restoreLinkman.setRestoreDate(new Date());
    return this.linkmanDAO.updateLinkmanByStaticParam(restoreLinkman);
  }
}